package model;


import utils.PriceUtils;
import java.util.*;

public class  Order {
    private int id;//订单id
    private String code;//订单号
    private float buy_price;//商品价格
    private int amount;// 商品数量
    private String user_name;// 用户名
    private User user;
    private int product_id;
    private int seller_id;
    private Goods goods;


    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getCode() {
        return code;
    }
    public void setCode(String code) { this.code = code; }

    public void setBuy_price(float buy_price) {
        this.buy_price = buy_price;
    }

    public float getBuy_price() {
        return buy_price;
    }
    public void setTotal(float buy_price) {
        this.buy_price = buy_price;
    }
    public int getAmount() {
        return amount;
    }
    public void setAmount(int amount) {
        this.amount = amount;
    }
    public User getUser() { return user; }
    public void setUser(User user) { this.user = user; }
    public String getUser_name() { return user_name; }
    public void setUser_name(String user_name){ this.user_name = user_name; }
    public int getProduct_id() { return product_id; }
    public void setProduct_id(int product_id) { this.product_id = product_id; }
    public int getSeller_id() { return seller_id; }
    public void setSeller_id(int seller_id) { this.seller_id = seller_id; }
    private Map<Integer,OrderItem> itemMap = new HashMap<Integer,OrderItem>();
    private List<OrderItem> itemList = new ArrayList<OrderItem>();
    public Goods getGoods() { return goods; }
    public void setGoods(Goods goods) { this.goods = goods; }

    public void setUsername(String username) {
        user = new User();
        user.setUsername(username);
    }
    public void addGoods(Goods g) {
        if(itemMap.containsKey(g.getId())) {
            OrderItem item = itemMap.get(g.getId());
            item.setAmount(item.getAmount()+1);
        }else {
            OrderItem item = new OrderItem(g.getPrice(),1,g,this);
            itemMap.put(g.getId(), item);
        }
        //若是此goods已经加入过订单，则只需让数量+1即可
        amount++;
        buy_price = PriceUtils.add(buy_price, g.getPrice());
    }

    public List<OrderItem> getItemList() {
        return itemList;
    }

    public void setItemList(List<OrderItem> itemList) {
        this.itemList = itemList;
    }

    public void lessen(int goodsid) {
        if(itemMap.containsKey(goodsid)) {
            OrderItem item = itemMap.get(goodsid);
            item.setAmount(item.getAmount()-1);
            amount--;
            buy_price = PriceUtils.subtract(buy_price, item.getPrice());
            if(item.getAmount()<=0) {
                itemMap.remove(goodsid);
            }
        }
    }
    public void delete(int goodsid)
    {
        if(itemMap.containsKey(goodsid)) {
            OrderItem item = itemMap.get(goodsid);
            buy_price = PriceUtils.subtract(buy_price, item.getAmount()*item.getPrice());
            amount-=item.getAmount();
            itemMap.remove(goodsid);
        }
    }

    public Map<Integer, OrderItem> getItemMap() {
        return itemMap;
    }

    public void setItemMap(Map<Integer, OrderItem> itemMap) {
        this.itemMap = itemMap;
    }

public Order() {
    super();
}

}
